package org.tio.sitexxx.service.service.tiosite;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.sitexxx.service.cache.CacheConfig;
import org.tio.sitexxx.service.cache.Caches;
import org.tio.sitexxx.service.model.main.Ad;
import org.tio.sitexxx.service.utils.QueryUtils;
import org.tio.sitexxx.service.vo.Const;
import org.tio.utils.cache.ICache;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;

/**
 * @author tanyaowu
 * 2016年7月22日 上午10:44:55
 */
public class AdService {
	@SuppressWarnings("unused")
	private static Logger			log	= LoggerFactory.getLogger(AdService.class);
	public static final AdService	me	= new AdService();
	private final Ad				dao	= new Ad().dao();

	/**
	 *
	 * @author tanyaowu
	 */
	public AdService() {
	}

	@SuppressWarnings("unchecked")
	public Page<Record> page(int pageNumber, int pageSize) {
		String cacheKey = "case_page" + "_" + pageNumber + "_" + pageSize;

		ICache cache = Caches.getCache(CacheConfig.TIME_TO_LIVE_SECONDS_5);
		Object obj = cache.get(cacheKey);
		if (obj != null) {
			return (Page<Record>) obj;
		}

		SqlPara sqlPara = dao.getSqlPara("case.page");
		Page<Record> ret = Db.paginate(pageNumber, pageSize, sqlPara);//.p.paginate(pageNumber, pageSize, sqlPara);
		cache.put(cacheKey, ret);
		return ret;
	}

	public Record getById(Integer id) {
		String sqlid = "ad.all";
		String cacheKey = sqlid + id;
		String tableName = "ad";
		return QueryUtils.findByIdWithCache(CacheConfig.TIME_TO_LIVE_MINUTE_5_LOCAL, cacheKey, Const.Db.TIO_SITE_MAIN, tableName, id, true);
	}

	/**
	 * 首页广告
	 * @return
	 * @author tanyaowu
	 */
	public List<Record> queryHomepage() {
		String sqlid = "ad.homepage";
		String cacheKey = sqlid;
		return QueryUtils.findWithCache(CacheConfig.TIME_TO_LIVE_MINUTE_5_LOCAL, cacheKey, Const.Db.TIO_SITE_MAIN, sqlid, true);
	}

}
